package core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;

/**
 * Class that represents a user object.
 * @author Anton
 */
public class User
{
    public static final int adminUser = 0;
    public static final int standardUser = 1;
    

    private long id;
    private String fname;
    private String lname;
    private String email;
    private String password;
    private int level;
    private Collection<Program> programs;
    private Collection<Course> courses;

    /**
     * Parameterless constructor.
     * Sets variables to null 
     * except programs and courses wich are instanciated.
     */
    public User()
    {
        level = standardUser;
        programs = new ArrayList<Program>();
        courses = new ArrayList<Course>();
    }
    /**
     * Program and courses are instanciated.
     * 
     * @param fname User's first name
     * @param lname User's last name
     * @param email User's email
     * @param password User's password
     * @param level User level (Use static final variables).
     */
    public User(String fname, String lname, String email,
            String password, int level)
    {
        this();
        this.fname = fname;
        this.lname = lname;
        this.email = email;
        this.password = password;
        this.level = level;
    }

    /**
     * Program and courses are instanciated.
     *
     * @param fname User's first name
     * @param lname User's last name
     * @param email User's email
     * @param password User's password
     * @param level User level (Use static final variables).
     */
    public User(long id, String fname, String lname, String email,
            String password, int level)
    {
        this();
        this.id = id;
        this.fname = fname;
        this.lname = lname;
        this.email = email;
        this.password = password;
        this.level = level;
    }

    /**
     * Get user's id.
     * @return User's id.
     */
    public long getId(){
        return id;
    }

    /**
     * Set user's id
     * @param id
     */
    public void setId(long id){
        this.id = id;
    }

    /**
     * Get user's first name.
     * @return user's first name.
     */
    public String getFname() {
        return fname;
    }

    /**
     * Set user's first name.
     * @param fname
     */
    public void setFname(String fname) {
        this.fname = fname;
    }

    /**
     * Get user's last name.
     * @return user's last name.
     */
    public String getLname() {
        return lname;
    }

    /**
     * Set user's last name.
     * @param lname
     */
    public void setLname(String lname) {
        this.lname = lname;
    }

    /**
     * Get user's email.
     * @return user's email.
     */
    public String getEmail() {
        return email;
    }

    /**
     * Set user's email.
     * @param email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Get user's password.
     * @return
     */
    public String getPassword() {
        return password;
    }

    /**
     * Set user's password.
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Get user's level.
     * @return user's level.
     */
    public int getLevel() {
        return level;
    }

    /**
     * Set user's level.
     * @param level
     */
    public void setLevel(int level) {
        this.level = level;
    }

    /**
     * Get user's programs
     * @return Collection with user's programs.
     */
    public Collection<Program> getPrograms()
    {
        return programs;
    }

    /**
     * Set user's programs.
     * @param programs collection with programs.
     */
    public void setPrograms(Collection<Program> programs)
    {
        this.programs = programs;
    }

    /**
     * Get user's courses
     * @return Collection with user's courses.
     */
    public Collection<Course> getCourses()
    {
        return courses;
    }

    /**
     * Set user's courses.
     * @param courses Collection with courses.
     */
    public void setCourses(Collection<Course> courses)
    {
        this.courses = courses;
    }

    /**
     * Add program to user
     * @param program program to be added.
     * @return true if successfully added, false else.
     */
    public boolean addProgram(Program program)
    {
        return programs.add(program);
    }

    /**
     * Remove program from user.
     * @param program program to be removed.
     * @return true if successfully removed, false else.
     */
    public boolean removeProgram(Program program)
    {
        return programs.remove(program);
    }

    /**
     * Add course to user.
     * @param course course to be added.
     * @return true if successfully added, false else.
     */
    public boolean addCourse(Course course)
    {
        return courses.remove(course);
    }

    /**
     * Remove course from user.
     * @param course course to be removed.
     * @return true if successfully removed, false else.
     */
    public boolean removeCourse(Course course)
    {
        return courses.remove(course);
    }

    /**
     * 
     * @param u (null returns false)
     * @return true if same object or same id. false else.
     */
    public boolean equals(User u)
    {
        if(u == null)
            return false;
        if(this == u)
            return true;
        return id == u.id;
    }

    /**
     * 
     * @return User as String.
     */
    @Override
    public String toString()
    {
        return  "id: " + id + "\n" +
                "fname: " + fname + "\n" +
                "lname: " + lname + "\n" +
                "email: " + email + "\n" +
                "password: " + password + "\n" +
                "level: " + level + "\n";
    }

    private class fnameComparator implements Comparator
    {
        public int compare(Object obj1, Object obj2)
        {
            User u1 = (User) obj1;
            User u2 = (User) obj2;
            int nameComp = u1.getFname().compareTo(u2.getFname());
            return ((nameComp == 0) ? u1.getLname().compareTo(u2.getLname()) : nameComp);
        }
    }
}
